// short version of document.getElementById
var obj = function(x){
	var result = document.getElementById(x);
	if(result == null) return false;
	else return result;
};

// ie reads xml header as first child, skip to nest sibling
var getRootNode = function(xmlDoc){
	var rootNode = xmlDoc.firstChild;
	if(rootNode.nodeName == "xml") rootNode = rootNode.nextSibling;
	return rootNode;
};

//dynamic load css and js file with callback on load
var loadCSS = function(path, callBack){
	var nNode = document.createElement('link');
	nNode.type = 'text/css';
	nNode.rel = 'stylesheet';
	nNode.href = path;
	// callback
	var ready = function(){
		if(callBack){
			if(typeof(callBack) == "string") eval(callBack)(nNode);
			else if(typeof(callBack) == "function") callBack(nNode);
		}
	};
	// add script tag to doc head
	nNode.onload = function(){ ready(); };
	nNode.onreadystatechange = function(){ if(nNode.readyState == "loaded") ready(); };
	document.getElementsByTagName("head")[0].appendChild(nNode);
};

//remove all child nodes
var removeChilds = function(holder){
	if(typeof(holder) == "object") while(holder.hasChildNodes()){ holder.removeChild(holder.lastChild); }
	else if(typeof(holder) == "string") { var holder = obj(holder); while(holder.hasChildNodes()){ holder.removeChild(holder.lastChild); } }
};

//get browser viewport size
var winSize = function(){
	var myWidth = 0, myHeight = 0;
	// Non-IE
	if(typeof(window.innerWidth) == 'number'){
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	// IE 5+
	else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	return new Object({'width':myWidth,'height':myHeight});
};

//elements by attribute name
var getElementsByAttribute = function(src, attName){
	var collection = new Array(); var tgs = src.getElementsByTagName('*');
	for(var t=0; t<tgs.length; t++) if(tgs[t].getAttribute(attName)) collection.push(tgs[t]);
	return collection;
};

// find real object position
var findPos = function(obj){
	var curleft = curtop = 0;
	if (obj.offsetParent){
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent){
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
};

// get vars from document.location
var getVars = function(url){
	if(typeof(url) == "undefined") url = window.location.href;
	var gets = new Object();
	try{ var arr = url.split('?')[1].split('&'); } catch(e){ return false };
	for(var t=0; t<arr.length; t++){
		var itm = arr[t].split('=');
		gets[itm[0]] = itm[1];
	}
	return gets;
};

// true if object is an array
var isArray = function(obj){
	if(obj != null){
		if(obj.constructor.toString().indexOf("Array") == -1) return false;
		else return true;
	} else return false;
};

var deselectAll = function(){
	if(document.selection) document.selection.empty();
	else if(window.getSelection) window.getSelection().removeAllRanges();
};

// return one enumeral member from js object, if empty object return false
function objectPop(o){
	if(typeof o == 'object'){
		for(var p in o){
			if (o[p] !== undefined && typeof o[p] != 'function'){
				var res = new Object();
				res.name = p;
				res.value = o[p];
				delete o[p];
				return res;
			}
		}
	}
	return false;
};
